
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

using Swaf;
using Swaf.BizObj;
using Swaf.Crypto;
using Swaf.Xml;

using MediaCache;

namespace MediaCacheAdmin
{
	/// <summary>
	/// Summary description for FileSysImageCacheAdmin.
	/// </summary>
	public class FileSysImageCacheUpdate : FileSysImageCache
	{
		public FileSysImageCacheUpdate(LineInfoXmlElement req, Swaf.Container.XmlCatalogContainerNode.NameInfo catalogName)
			:base(req, catalogName)
		{
			//Intentionally left blank
		}

		public override bool deleteImage(string name)
		{
			string fullName = Cmn.pathCombine(this.OrigionalImagesLocation, name);
			FileInfo fi = getRealNameFromNameWithoutSizeInfo(new FileInfo(fullName));
			if(fi.Exists)
			{
				fi.Delete();
				foreach(IBizObj scaleOptions in (IBizObjList)this.m_cacheOptions.get("scalings"))
                    foreach (IBizObj imgAdjInfo in (IBizObjList)scaleOptions.get("adjustments"))
                    {
                        fullName = getCacheImageName(name, scaleOptions, imgAdjInfo);
                        fi = new FileInfo(fullName);
                        if (fi.Exists)
                            fi.Delete();
                    }
				return true;
			}
			return false;
		}


		public override bool moveImage(string name, string newLocation)
		{
			string fullName = Cmn.pathCombine(this.OrigionalImagesLocation, name.Replace(':', '\\'));
			FileInfo fi = getRealNameFromNameWithoutSizeInfo(new FileInfo(fullName));
			string newFullName = Cmn.pathCombine(this.OrigionalImagesLocation, newLocation);
			newFullName = Cmn.pathCombine(newFullName, fi.Name);
			fi.MoveTo(newFullName);
			return true;
		}

		public override bool addImage(IBizObj img)
		{
			string fullName = Cmn.pathCombine(this.OrigionalImagesLocation, ((string)img.get("file.name")).Replace(':','\\'));
			this.verifyDirectory(Path.GetDirectoryName(fullName));
			FileInfo fi = new FileInfo(fullName);
			FileStream fs = fi.OpenWrite();
			Binary bin = (Binary)img.get("file.data");
			byte[] imgBytes = bin.Bytes;
			//if(myCrypto != null)
			//	imgBytes = myCrypto.encrypt(imgBytes);
            if (myCrypto != null)
            {
                CryptoStream streamOut = myCrypto.createCryptoStream(fs) as CryptoStream;
                streamOut.Write(imgBytes, 0, imgBytes.Length);
                streamOut.FlushFinalBlock();
                streamOut.Dispose();
            }
            else
    			fs.Write(imgBytes, 0, imgBytes.Length);

            fs.Dispose();
            bin.Dispose();
			
			return true;
		}
	}
}
